package com.sdx.console.promote.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ibeetl.admin.console.service.OrgConsoleService;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.DateUtil;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.AuthorizedBaesController;
import com.ibeetl.admin.core.web.JsonResult;
import com.sdx.common.utils.JacksonUtils;
import com.sdx.common.utils.Utils;
import com.sdx.console.promote.entity.ChannelAuthorization;
import com.sdx.console.promote.service.ChannelAuthorizationService;
import com.sdx.console.system.entity.ChannelSet;
import com.sdx.console.system.service.ChannelSetService;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 邀请码 接口
 */
@RequestMapping("/promote/channelAuthorization")
@Controller
public class ChannelAuthorizationController extends AuthorizedBaesController{
	private static final Logger log = LoggerFactory.getLogger(ChannelAuthorizationController.class);
    private static final String MODEL = "/promote/channelAuthorization";


    @Resource
	private ChannelAuthorizationService channelAuthorizationService;
    
    @Resource
    private ChannelSetService channelSetService;

    @Resource
    private OrgConsoleService orgConsoleService;


    @Resource
    private CorePlatformService corePlatformService;

    private void setPageParam(ModelAndView view,HttpServletRequest request) {
	    CoreUser user = getLoginUser(request);
	    Integer accessType = corePlatformService.getAccessType(request);
	    List<ChannelSet> channelList = channelSetService.selectAuthCodeChannels(accessType, user.getOrgId(),user.getTenantId());
	    view.addObject("accessType",accessType);
    	view.addObject("channelCodeList",channelList);
        view.addObject("orgList",orgConsoleService.getOrgs());
    }
    /* 页面 */

    @GetMapping("/index.do")
    @Function("promote.channelAuthorization.query")
    @ResponseBody
    public ModelAndView index(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(MODEL+"/index.html") ;
        setPageParam(view,request);
        return view;
    }

    @GetMapping("/edit.do")
    @Function("promote.channelAuthorization.edit")
    @ResponseBody
    public ModelAndView edit(String auCode,HttpServletRequest request) {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
	    setPageParam(view,request);
        ChannelAuthorization channelAuthorization = channelAuthorizationService.queryById(auCode);
        view.addObject("channelAuthorization", channelAuthorization);
		try {
			view.addObject("channelAuthorizationJson", JacksonUtils.toJsonString(channelAuthorization,JacksonUtils.DF1));
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
        return view;
    }

    @GetMapping("/add.do")
    @Function("promote.channelAuthorization.add")
    @ResponseBody
    public ModelAndView add(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
	    ChannelAuthorization channelAuthorization = new ChannelAuthorization();
	    channelAuthorization.setAuCode(Utils.uuid());
	    view.addObject("channelAuthorization", channelAuthorization);
	    setPageParam(view,request);
        return view;
    }

    /* ajax json */

    @PostMapping("/list.json")
    @Function("promote.channelAuthorization.query")
    @ResponseBody
    public JsonResult<PageQuery> list(ChannelAuthorization condtion,HttpServletRequest request)
    {
	    CoreUser user = getLoginUser(request);
	    Integer accessType = corePlatformService.getAccessType(request);
        PageQuery page = condtion.getPageQuery();
	    if (accessType.intValue()>1) {
	    	if (accessType.intValue()==3) {
	    		condtion.setOrgId(user.getOrgId().intValue());
		    }
		    channelAuthorizationService.queryByCondition(page);
	    }
        return JsonResult.success(page);
    }

    private JsonResult save(boolean isNew,ChannelAuthorization channelAuthorization,HttpServletRequest request
	) {
    	JsonResult result = JsonResult.success();
		String dir = DateUtil.now("yyyyMMdd");
    	String subPath = "/images/cover/"+dir+"/";
		String bucketName = "sdxcomm";
		CoreUser user = getLoginUser(request);

    	if (isNew) {
			channelAuthorization.setCreatedBy(user.getId().intValue());
			channelAuthorization.setCreatedName(user.getName());
			channelAuthorization.setCreatedTime(new Date());
			channelAuthorizationService.save(channelAuthorization);
		} else {
			channelAuthorization.setUpdatedBy(user.getId().intValue());
			channelAuthorization.setUpdatedName(user.getName());
			channelAuthorization.setUpdatedTime(new Date());
			channelAuthorizationService.updateTemplate(channelAuthorization);
		}
		return result;
	}
    @PostMapping("/add.json")
    @Function("promote.channelAuthorization.add")
    @ResponseBody
    public JsonResult add(@Validated(ValidateConfig.ADD.class)ChannelAuthorization channelAuthorization,HttpServletRequest request
		)
    {

        return save(true,channelAuthorization,request
		);
    }

    @PostMapping("/update.json")
    @Function("promote.channelAuthorization.edit")
    @ResponseBody
    public JsonResult<String> update(@Validated(ValidateConfig.UPDATE.class)  ChannelAuthorization channelAuthorization,HttpServletRequest request
		) {
		return save(false,channelAuthorization,request
		);
    }


   
    @GetMapping("/view.json")
    @Function("promote.channelAuthorization.query")
    @ResponseBody
    public JsonResult<ChannelAuthorization>queryInfo(String auCode) {
        ChannelAuthorization channelAuthorization = channelAuthorizationService.queryById( auCode);
        return  JsonResult.success(channelAuthorization);
    }

    @PostMapping("/delete.json")
    @Function("promote.channelAuthorization..del")
    @ResponseBody
    public JsonResult delete(String ids) {
		List<String> idList = Utils.strToList(ids,",");
        channelAuthorizationService.batchDelChannelAuthorization(idList);
        return JsonResult.success();
    }
    

}
